package com.ccsc.auth.controller;

import com.ccsc.auth.entity.*;
import com.ccsc.auth.entity.vo.GetSysVo;
import com.ccsc.auth.entity.vo.SystemVo;
import com.ccsc.auth.utils.BaseResponse;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by yyangs on 2017/2/21.
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Value("#{configProperties['url']}")
    private String url;

    //获取目录
    @RequestMapping(value = "/getSys", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getSys() throws Exception {
        try {
            GetSysVo result = new GetSysVo();
            ShiroSysUser sessionUser = getSessionUser();
            if(sessionUser != null) {
                result.setUser(sessionUser.getAccount());
                result.setUserManagerShow("0");
                //判断是否是超级管理员
                if (sessionUser.getAccount().equals("superAdmin")){
                    result.setUserManagerShow("1");

                    result.setUserManagerUrl(url);
                }
                //封装系统
                List<SystemVo> list = Lists.newArrayList();
                for (ShiroSysSystem shiroSysSystem : sessionUser.getSystem()){
                    SystemVo systemVo = new SystemVo();
                    systemVo.setId(String.valueOf(shiroSysSystem.getSystemId()));
                    systemVo.setName(shiroSysSystem.getSystemName());
                    systemVo.setImgUrl("");
                    systemVo.setUrl(shiroSysSystem.getSystemUrl());
                    list.add(systemVo);
                }
                result.setSystem(list);
            }

            return this.ok(result);
        } catch (Exception ex) {
            ex.printStackTrace();
            return this.error(500);
        }
    }

}
